﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class Home : System.Web.UI.Page
    {

        private mamnontuoitho7.AppCode.Article _article = new mamnontuoitho7.AppCode.Article();
        private ArticleType _articleType = new ArticleType();
        protected General _general = new General();

        protected Conversation _conversation = new Conversation();
        private KidWall _kidWall = new KidWall();
        protected DataTable _dHome;

        protected String _currentSubArticleID = "";
        protected Product _product = new Product();
        protected Comment _comment = new Comment();

        protected DataTable _dt;
        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalArticleNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        protected String _notice = "";
        protected String _currentLink = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            string temp = "1";
            Session["Common_articleTypeName"] = "";
            if (Request.QueryString["SubArticle"] != null)
            {
                temp = Request.QueryString["SubArticle"].ToString();
                Match match = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                if (temp.Length <= 4 && match.Success)
                {
                    bool check = false;
                    try
                    {
                        check = _articleType.checkArticleSuperior(temp, Session["Common_articleTypeID"].ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorPage);
                    }

                    if (check)
                    {
                        try
                        {
                            _currentSubArticleID = temp;
                            Session["Common_articleTypeName"] = _articleType.GetOneArticleTypeByID(_currentSubArticleID)["ArticleTypeName"].ToString();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Response.Redirect(ConstantClass._errorPage);
                        }
                    }
                    else { Response.Redirect("Home.aspx"); }
                }
                else
                {
                    Response.Redirect("Home.aspx");
                }


                try
                {
                    /////////////////////// paging
                    _totalArticleNumber = _article.TotalArticleOfOneType(_currentSubArticleID).GetValueOrDefault();
                    _totalPages = _general.Divide(_totalArticleNumber, ConstantClass._pageStepArticle);
                    _totalBars = _general.Divide(_totalPages, ConstantClass._barStepArticle);

                    if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                    {
                        string temp2 = Request.QueryString["page"].ToString();
                        Match match2 = Regex.Match(temp2, @"([0-9])$", RegexOptions.IgnoreCase);

                        if (temp2.Length <= 4 && match2.Success)
                        {
                            _pageflag = Convert.ToInt16(temp2);// trang hiện hành
                            if (_pageflag <= _totalPages)
                            {
                                _currentbar = _general.Divide(_pageflag, ConstantClass._barStepArticle);
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                            else
                            {
                                _pageflag = 1;
                                _currentbar = 1;
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                        }
                    }
                    if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                    {
                        _currentbar = 1;
                        _pageflag = 1;
                        Response.Cookies["CurrentBar"].Value = "1";
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                    {
                        _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                        if (_currentbar < _totalBars)
                        {
                            _currentbar += 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepArticle + 1;

                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                    {
                        _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                        if (_currentbar > 1)
                        {
                            _currentbar -= 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepArticle + 1;
                    }

                    _dt = _article.GetArticlePaged(_pageflag, ConstantClass._pageStepArticle, _currentSubArticleID);
                    _beginPage = (_currentbar - 1) * ConstantClass._barStepArticle + 1;
                    _endPage = _currentbar * ConstantClass._barStepArticle;

                    if (_endPage > _totalPages)
                    {
                        _endPage = _totalPages;
                    }
                    ///////////////////////End paging
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorPage);
                }
            }

            if (_currentSubArticleID == "")
            {
                Session["Common_articleTypeName"] = "Giới thiệu";
            }

            if (_currentSubArticleID == ConstantClass._ThanhTich || _currentSubArticleID == ConstantClass._SoDoToChuc || _currentSubArticleID == ConstantClass._NhanSu)
            {
                _currentSubArticleID = "";
            }

            _dHome = _article.GetSomeTopArticleContentByType("1", temp);
           // _drHome = _article.GetSomeTopArticleContentByType("1", "1").Rows[0];

            if (Request.QueryString["notice"] != null && Request.QueryString["notice"].ToString() != "")
            {
                _notice = Request.QueryString["notice"].ToString();
                _currentLink = Session["Common_currentPageLink"].ToString();

                //This code to stop repeat notice when login fail on Home page
                if (_currentLink.IndexOf("Home.aspx?") != -1)
                {
                    _currentLink = _currentLink.Substring(0, _currentLink.IndexOf("?"));
                }

            }



            Session["Common_articleTypeID"] = "1";

        }

    }
}